package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.DetailOrderDao;
import com.java2007.hotel.dao.FoodDao;
import com.java2007.hotel.dao.impl.FoodDaoImpl;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.DetailOrder;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.utils.JdbcUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 菜品业务层实现类
 *
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
public class FoodServiceImpl implements FoodService {
    private FoodDao foodDao = (FoodDao) BeanFactory.getBean(Constant.FOOD_DAO_IMPL);
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean(Constant.FOOD_TYPE_SERVICE_IMPL);
    private DetailOrderDao detailOrderDao = (DetailOrderDao) BeanFactory.getBean(Constant.DETAIL_ORDER_DAO_IMPL);

    /**
     * 查询所有菜品
     *
     * @return
     */
    @Override
    public List<Food> selectAll() throws SQLException {

        List<Food> foods = foodDao.selectAll();
        for (int i = 0; i < foods.size(); i++) {
            System.out.println(foods.get(i));
        }

        for (int i = 0; i < foods.size(); i++) {
            Integer cuisineId = foods.get(i).getCuisineId();
            System.out.println(cuisineId);
            FoodType foodType = foodTypeService.selectFoodTypeById(cuisineId);
            System.out.println(foodType);
            foods.get(i).setCuisine(foodType.getName());
        }
        return foods;
    }

    /**
     * 根据名字模糊查询
     *
     * @param name
     * @return
     */
    @Override
    public List<Food> selectFoodByName(String name) throws SQLException {
        List<Food> foods = foodDao.selectFoodByName(name);
        for (int i = 0; i < foods.size(); i++) {
            foods.get(i).setCuisine(foodTypeService.selectFoodTypeById(foods.get(i).getCuisineId()).getName());
        }
        return foods;
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public Food selectFoodById(int id) throws SQLException {
        Food food = foodDao.selectFoodById(id);
        food.setCuisine(foodTypeService.selectFoodTypeById(food.getCuisineId()).getName());
        return food;
    }

    /**
     * 根据id删除菜品
     *
     * @param id
     * @return
     */
    @Override
    public int deleteFoodById(int id) {
        List<DetailOrder> detailOrders = null;
//        try {
//            detailOrders = detailOrderDao.selectByFoodId(id);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        if (detailOrders.size() != 0){
//            return 2;
//        }
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodDao.deleteFoodById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        } finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 修改菜品
     *
     * @param food
     * @return
     */
    @Override
    public int updateFood(Food food) {
        Date date = new Date();
        food.setUpdateDate(date);
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodDao.updateFood(food);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        } finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 增加一个菜品
     *
     * @param food
     * @return
     */
    @Override
    public int addFood(Food food) {
        Date date = new Date();
        food.setCreateDate(date);
        JdbcUtils.startTransaction();
        int i = 0;
        try {
            i = foodDao.addFood(food);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        } finally {
            JdbcUtils.commit();
        }
        return i;
    }

    /**
     * 根据菜系id删除菜品
     *
     * @param cuisineId
     * @return
     */
    @Override
    public int deleteFoodByCuisineId(int cuisineId) throws SQLException {
        return foodDao.deleteFoodByCuisineId(cuisineId);
    }

    /**
     * 根据名字查询一个
     * @param name
     * @return
     */
    @Override
    public Food selectFoodByNameOne(String name) throws SQLException {
        return foodDao.selectFoodByNameOne(name);
    }

    /**
     * 分页查询
     * @param start
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Food> selectLimit(int start, int pageSize) throws SQLException {
        return foodDao.selectLimit(start,pageSize);
    }
    /**
     * 根据菜系id查询菜品
     * @param cuisineId
     * @return
     */
    @Override
    public List<Food> selectFoodBy(int cuisineId) throws SQLException {
        return foodDao.selectFoodBy(cuisineId);
    }
    /**
     * 根据菜系id分页查询菜品
     * @param cuisineId
     * @return
     */
    @Override
    public List<Food> selectFoodBy(int cuisineId, int start, int pageCount) throws SQLException {
        return foodDao.selectFoodBy(cuisineId,start,pageCount);
    }

    /**
     * 根据名字模糊分页查询
     * @param name
     * @return
     */
    @Override
    public List<Food> selectFoodByNameLimit(String name, int start, int pageCount) throws SQLException {
        return foodDao.selectFoodByNameLimit(name,start,pageCount);
    }
}
